#pragma once

#include "exports.hpp"

#include "Plugin.h"
#include "DataReader.hpp"




class CRAWLER_API DataProcessor
	: public Plugin
{
public:

	typedef boost::shared_ptr<DataProcessor>         Ptr;
	typedef boost::shared_ptr<const DataProcessor>   const_Ptr;

	typedef boost::posix_time::time_duration         time_duration;
	typedef boost::posix_time::ptime                 ptime;

private:

	DataReader::Ptr        m_provider;

	boost::optional<ptime>   m_wakeUp; //< The time this processor wants to wake up again

public:

	/**
	 * Creates a DataProcessor that fetches replies from the given DataProvider.
	 */
	DataProcessor();
	virtual ~DataProcessor();



	/**
	 * Attach the given DataProvider to this DataProcessor.
	 */
	void attach(DataReader::Ptr provider);



	/**
	 * Unpauses the DataProcessor.
	 */
	virtual void unpause();



	/**
	 * Tests if this processor wants to sleep or not.
	 */
	bool sleep() const;

	/**
	 * @returns the time this processor wants to wake up again
	 */
	boost::optional<ptime> wakeUp() const;



	/**
	 * Processes the next reply (if available).
	 */
	void process();

protected:

	/**
	 * Process the given data.
	 */
	virtual void process(const Reply &reply) = 0;

	/**
	 * Sleeps for at least the given duration.
	 */
	void sleep(time_duration duration);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
